/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication6.UI;

/**
 *
 * @author meydlo
 */
import javaapplication6.ControlLayer.CustomerControl;
import javaapplication6.DomainLayer.Customer;
public class CustomerUI {

    private CustomerControl customerControl;
    private Listener listener=new Listener();
    

    public CustomerUI(){
        customerControl=new CustomerControl();
    }
    public void printChoices()
    {
        System.out.println(
                "\n (1) create customer"+
                "\n (2) print customer info"+
                "\n (3) update customer"+
                "\n (4) delete customer"+
                "\n (5) print choices"+
                "\n (6) print customerList"+
                "\n (7) set discount for business/private customers"+
                "\n (8) main menu"
                );
    }
    public void CustomerManager()
    {
        boolean exit=false;
        while(exit==false){
            printChoices();
            int number=listener.getLineInt();
            switch(number){
                case 1:{
                    createCustomer();
                    break;
                }
                case 2:{
                    printCustomerInfo();
                    break;
                }
                case 3:{
                    updateCustomer();
                    break;
                }
                case 4:{
                    deleteCustomer();
                    break;
                }
                case 5:{
                    printChoices();
                    break;
                }
                case 6:{
                    printCustomerList();
                    break;
                }
                case 7:{
                    setDiscountToCustomers();
                    break;
                }
                case 8:{
                    exit=true;
                    break;
                }
                case -1:{
                    System.out.println("Please write a number");
                    break;
                }
                default:{
                    System.out.println("Valid rank id: 1-7");
                }
            }
        }
    }

    public void createCustomer()
    {
        String name=getNameUI();
        String phoneNo=getPhoneNoUI();
        String address=getAddressUI();
        boolean business=getBusinessUI();

        customerControl.createCustomer(name, phoneNo, address, business);
        
        System.out.println("Customer successfully added");
    }

    protected Customer getCustomerByIDui()
    {
        Customer customer;
        do{
            long customerId=getCustomerIdUI();
            customer=customerControl.findCustomer(customerId);
            if(customer==null){
                System.out.println("SellProduct wasn't found");
                System.out.println("try another id? (y/n):");
                if(listener.getLineBoolean()==false) break;
            }
        }while(customer==null);
        return customer;
       
    }

    public void printCustomerInfo()
    {
        try{
            Customer customer=getCustomerByIDui();
            customer.printCustomerInfo();
        }
        catch(Exception e){
        }
    }

    public void printCustomerList()
    {
        customerControl.printCustomerList();
    }

    public void updateCustomer()
    {
        boolean exit=false;
        Customer customer=getCustomerByIDui();
        do{
            if(customer==null) break;
            System.out.println("what you need to change?: ");
            printChoiceOfChanges();
            int line=listener.getLineInt();
            switch(line){
                case 1:{
                    String name=getNameUI();
                    customer.setName(name);
                    break;
                }
                case 2:{
                    String address=getAddressUI();
                    customer.setAddress(address);
                    break;
                }
                case 3:{
                    String phoneNo=getPhoneNoUI();
                    customer.setPhoneNo(phoneNo);
                    break;
                }
                case 4:{
                    double discount=getDiscountUI();
                    customer.setDiscount(discount);
                    break;
                }
                case 5:{
                    boolean businessC=getBusinessUI();
                    customer.setBusiness(businessC);
                    break;
                }
                case 6:{
                    exit=true;
                    break;
                }
            }

        }while(exit==false);

    }


    public void printChoiceOfChanges()
    {
        System.out.println("\n(1) name: "+
                "\n(2) address: "+
                "\n(3) phoneNo: "+
                "\n(4) discount: "+
                "\n(5) isBusiness(y/n): "+
                "\n(6) exit"
                );
    }

    public void deleteCustomer()
    {
        try{
            Customer cusotmer=getCustomerByIDui();
            long idOdCustomer=cusotmer.getId();
            customerControl.deleteCustomer(idOdCustomer);
            System.out.println("Customer successful deleted");
        }
        catch(Exception e){
        }
    }

    public void setDiscountToCustomers()
    {
        System.out.println("discaunt for business kind of person? (y/n)");
        boolean isBusiness=listener.getLineBoolean();
        System.out.println("set discount: ");
        double discount=listener.getLineDouble();
        customerControl.setDiscount(isBusiness, discount);
    }

    public String getNameUI()
    {
        System.out.println("name: ");
        String name=listener.getLineString();
        return name;
    }

    public String getAddressUI()
    {
        System.out.println("address:");
        String address=listener.getLineString();
        return address;
    }

    public String getPhoneNoUI()
    {
        System.out.println("phone number: ");
        String phoneNo=listener.getLineString();
        return phoneNo;
    }

    public boolean getBusinessUI()
    {
        System.out.println("is customer business? (y/n): ");
        boolean business=listener.getLineBoolean();
        return business;
    }

    public long getCustomerIdUI()
    {
        System.out.println("Write id of cusotmer");
        long customerId=listener.getLineInt();
        return customerId;
    }

    public double getDiscountUI()
    {
        System.out.println("Write discount of cusotmer");
        double customerId=listener.getLineInt();
        return customerId;
    }


}
